package com.day04;

import java.util.Arrays;
import java.util.Random;

/**
 * 2022年4月6日 14:18:43
 */
public class 选择排序04 {
    public static void selectionSort(int arr[]) {
        int index;
        for (int i = 0; i < arr.length - 1; i++) {
            index = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[index]) {
                    index = j;
                }
            }
            if (index != i) {
                int temp = arr[index];
                arr[index] = arr[i];
                arr[i] = temp;
            }

        }
    }

    public static void main(String[] args) {
        Random random = new Random();
        int arr[] = new int[100];
        int arr1[] = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(1000);
        }
        System.arraycopy(arr, 0, arr1, 0, arr.length);
        Arrays.sort(arr1);
        selectionSort(arr);
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == arr1[i]) {
                flag = flag && true;
            } else {
                flag = false;
            }
        }
        System.out.println("选择排序结果是否正确: " + (flag ? "√" : "×"));
    }
}
